package com.mifengs.mall.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.mifengs.mall.util.UIUtil;

/**
 * Created by lihaoqi on 2017/11/1.
 */

public class IndicatorView extends View {

    private int mWidth;
    private int mHeight;
    private int mTotalSize = 1;
    private int mCurrentIndex = 0;
    private int mSpacePx = UIUtil.dp2px(6);  //间距
    private int mCircleRadius = UIUtil.dp2px(2); //圆点半径
    private int mLineRoundCorners = UIUtil.dp2px(2);
    private Paint mNormalPaint;
    private Paint mSelectedPaint;

    public IndicatorView(Context context) {
        this(context,null);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs ,0);
    }

    public IndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    public void setTotalSize(int totalSize) {
        this.mTotalSize = totalSize;
        invalidate();
    }

    public void setCurrentIndex(int currentIndex) {
        this.mCurrentIndex = currentIndex;
        invalidate();
    }

    private void init() {
        mNormalPaint = new Paint();
        mNormalPaint.setAntiAlias(true);
        mNormalPaint.setFilterBitmap(true);
        mNormalPaint.setColor(Color.parseColor("#FFFFCC99"));
        mSelectedPaint = new Paint();
        mSelectedPaint.setAntiAlias(true);
        mSelectedPaint.setFilterBitmap(true);
        mSelectedPaint.setColor(Color.parseColor("#FFFF7F00"));

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawRect(0,0,mWidth,mHeight,paint);
        Point p = calculate();
        if(p!=null){
            for(int i=0;i<mTotalSize;i++){
                if(i!=mCurrentIndex){
                    canvas.drawCircle(p.x,p.y,mCircleRadius,mNormalPaint);
//                    RectF rectF = new RectF(p.x-mCircleRadius,p.y-mCircleRadius/2,p.x+mCircleRadius,p.y+mCircleRadius/2);
//                    canvas.drawRoundRect(rectF,mLineRoundCorners,mLineRoundCorners,mNormalPaint);
                }else{
                    canvas.drawCircle(p.x,p.y,mCircleRadius,mSelectedPaint);
                }
                p.x = p.x + mSpacePx + 2*mCircleRadius;
            }
        }
    }

    /**
     * 计算起始点的坐标，根据totalSize 算出 点一共占的长度
     */
    private Point calculate(){
        //几个间距长度
        int disPx = mSpacePx*(mTotalSize-1);
        //几个圆点的长度
        int totalCicleL = mTotalSize*mCircleRadius*2;
        //全部长度
        int totalLong = disPx + totalCicleL;

        if(totalLong<=mWidth){
            Point p = new Point();
            p.x =((mWidth-totalLong)/2)+mCircleRadius;
            p.y = mHeight/2;
            return  p;
        }else {
            return null;
        }


    }

}
